package com.sansi.pencilbook.controller;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.BookStore;
import com.sansi.pencilbook.entity.User;
import com.sansi.pencilbook.service.AdminServer;
import com.sansi.pencilbook.service.BookStoreServer;
import com.sansi.pencilbook.service.UserServer;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明: 判断是否是否登录
 *
 * @author lsy13
 */
public class VerifyController {

    @Autowired
    private UserServer userServer;

    @Autowired
    private AdminServer adminServer;

    @Autowired
    private BookStoreServer bookStoreServer;

    /**
     * 判断用户是否登录
     * @param request
     * @return
     */
    public boolean checkUser(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        try {
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            Result result = userServer.getUserByAccount(userAccount);
            return result.isSuccess();
        }catch (NullPointerException ignored) {
            System.out.println("cookie为空");
            return false;
        }
    }

    public boolean checkUser(String account) {
        Result result = userServer.getUserByAccount(account );
        return result.isSuccess();
    }

    /**
     * 判断管理员是否登录
     * @param request
     * @return
     */
    public boolean checkAdmin(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String adminAccount = null;
        try {
            for (Cookie cookie : cookies) {
                if ("adminAccount".equals(cookie.getName())) {
                    adminAccount = cookie.getValue();
                }
            }
            Result result = adminServer.getAdminByAccount(adminAccount);
            return result.isSuccess();
        }catch (NullPointerException ignored) {
            System.out.println("cookie为空");
            return false;
        }
    }


    /**
     * 判断管理员是否登录
     * @param request
     * @return
     */
    public boolean checkStore(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String storeId = null;
        try {
            for (Cookie cookie : cookies) {
                if ("storeId".equals(cookie.getName())) {
                    storeId = cookie.getValue();
                }
            }
            Result result = bookStoreServer.getStore(Integer.parseInt(storeId));
            return result.isSuccess();
        }catch (NullPointerException ignored) {
            System.out.println("cookie为空");
            return false;
        }
    }

    /**
     * 通过cookie获取书店Id
     * @param request
     * @return
     */
    public int getStore(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String storeId = null;
        try {
            for (Cookie cookie : cookies) {
                if ("storeId".equals(cookie.getName())) {
                    storeId = cookie.getValue();
                }
            }
            Result result = bookStoreServer.getStore(Integer.parseInt(storeId));
            BookStore store = (BookStore) result.getData();
            return result.isSuccess() ? store.getStoreId() : 0;
        }catch (NullPointerException ignored) {
            System.out.println("cookie为空");
            return 0;
        }
    }

    /**
     * 通过cookie获取UserAccount
     * @param request
     * @return
     */
    public String getUserAccount(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        String userAccount = null;
        try {
            for (Cookie cookie : cookies) {
                if ("userAccount".equals(cookie.getName())) {
                    userAccount = cookie.getValue();
                }
            }
            Result result = userServer.getUserByAccount(userAccount);
            User user = (User) result.getData();
            return result.isSuccess() ? String.valueOf(user.getUserAccount()) : "";
        }catch (NullPointerException ignored) {
            System.out.println("cookie为空");
            return "";
        }
    }
}
